﻿using System;
using System.Collections.Generic;
using System.Text;

namespace BL_Recorder
{
   public class Game
    {
        public int GetScore()
        {
           return socreForFrem(GetCurrentFrame()-1);
        }

        public void Add(int num)
        {
            itsThrows[itsTurrentThrow++] = num;
            itsScore += num;
            AdjustCurrentFrame(num);
        }
        private void AdjustCurrentFrame(int num)
        {
            if (firstThrowInFrame == true)
            {
                if (num == 10)
                {
                    itsCurrentFrame++;
                    firstThrowInFrame = true;
                }
                else
                {
                    firstThrowInFrame = false;
                }
               
            }
            else
            {
                firstThrowInFrame = true;
                itsCurrentFrame++;
            }
            itsCurrentFrame = Math.Min(11, itsCurrentFrame);
        }
        /// <summary>
        /// 计算指定轮数的成绩
        /// </summary>
        /// <param name="theFrem"></param>
        /// <returns></returns>
        
        public int socreForFrem(int theFrem)
        {
            int socre = 0;
           ball = 0;
            for (int crrentFrem=0;crrentFrem<theFrem;crrentFrem++)
            {
              
                if (Strike())
                {
                    socre += 10 + NextTowBallsForStrike();
                    ball++;
                }
                else if(Spare())
                    {

                    socre += 10 + NextBallForSpare();
                    ball += 2;

                }
                else
                {
                    socre += TowBallInFrame();
                    ball += 2;

                }  
            }
            return socre;
        }
        /// <summary>
        /// 非全中情况下的计算分数
        /// </summary>
        /// <returns></returns>
        //private  int HandleSecondThrow()
        //{
        //    int score = 0;
        //    if (Spare())
        //    {
        //        ball += 2;
        //        score += 10 + NextBall();
        //    }
        //    else
        //    {
               
        //        score += TowBallInFrame();
        //        ball += 2;
        //    }
              
        //    return score;

        //}
        /// <summary>
        /// 判断全中
        /// </summary>
        /// <returns></returns>
        private bool Strike()
        {
            return itsThrows[ball] == 10;
        }
        /// <summary>
        /// 判断补中
        /// </summary>
        /// <returns></returns>
        private bool Spare()
        {
            return (itsThrows[ball]+itsThrows[ball+1] )== 10;
        }
        /// <summary>
        /// 全中奖励分数
        /// </summary>
        /// <returns></returns>
        private  int NextTowBallsForStrike()
        {
            return itsThrows[ball+1] + itsThrows[ball + 2];
        }
        /// <summary>
        /// 补中奖励分数
        /// </summary>
        /// <returns></returns>
        private int NextBallForSpare()
        {
            return itsThrows[ball+2];
        }
        /// <summary>
        /// 无补中、全中分数
        /// </summary>
        /// <returns></returns>
        private int TowBallInFrame()
        {
            return itsThrows[ball]+itsThrows[ball +1];
        }

        public int  GetCurrentFrame()
        {
            return itsCurrentFrame;
        }
        private int ball;
        private int firstThrow;
        private int secondThrow;

        private bool firstThrowInFrame = true;
        private int itsCurrentFrame = 1;
        private int itsScore;
        private int[] itsThrows = new int[21];
        private int itsTurrentThrow=0;
    }
}
